package echat.future.cn.echat.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.nim.avchatkit.AVChatUserData;
import com.netease.nim.avchatkit.activity.AVChatActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.dialog.domain.Theme;
import echat.future.cn.echat.dialog.domain.YuyueData;
import echat.future.cn.echat.home.AnchorHomeActivity;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.view.RechargeActivity;
import lumingweihua.future.cn.baselibgxh.adapter.BaseViewHolder;
import lumingweihua.future.cn.baselibgxh.adapter.CommonAdapter;
import lumingweihua.future.cn.baselibgxh.base.FutureApi;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;
import lumingweihua.future.cn.baselibgxh.network.AbstractRequest;
import lumingweihua.future.cn.baselibgxh.network.BeanRequest;
import lumingweihua.future.cn.baselibgxh.network.JsonElementRequest;
import lumingweihua.future.cn.baselibgxh.network.ReqTag;
import lumingweihua.future.cn.baselibgxh.network.entity.DefaultMamahaoServerError;
import lumingweihua.future.cn.baselibgxh.network.entity.MamaHaoServerError;
import lumingweihua.future.cn.baselibgxh.utils.ImageUtils;
import lumingweihua.future.cn.baselibgxh.utils.MD5;
import lumingweihua.future.cn.baselibgxh.utils.SharedPreference;

/**
 *
 */
public class YuyueDialog extends Dialog implements OnClickListener,AbstractRequest.ApiCallBack {
    private OnYuyueDialogClickListener listener;
    public YuyueDialog(Context context, String anchor_id,
                       OnYuyueDialogClickListener listener) {
        super(context, R.style.BMF_Dialog);
        this.listener = listener;
        this.anchor_id = anchor_id;
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.yuyue_dialog);

        findViewById(R.id.bt_yes).setOnClickListener(this);
        findViewById(R.id.iv_close).setOnClickListener(this);

        requestDatas();
    }

    private String anchor_id;

    private void requestDatas(){
        BeanRequest<YuyueData, MamaHaoServerError> req = new BeanRequest<>(
                getContext(),
                "/api/user/theme_list",
                this,
                YuyueData.class);

        req.setParam("token", UserHelper.getToken());
        req.setParam("anchor_id",anchor_id);

        req.setTag(new ReqTag.Builder().build(1));
        FutureApi.getInstance().add(req);
    }

    private void requestYuyue(){
        JsonElementRequest<DefaultMamahaoServerError> req = new JsonElementRequest<DefaultMamahaoServerError>(
                getContext(), "/api/user/do_appointment", this);

        req.setParam("token", UserHelper.getToken());
        req.setParam("anchor_id",anchor_id);

        String theme_id = list.get(currentPos).getTheme_id();
        req.setParam("theme_id",theme_id);

        String key = "9b4aaf?&&##664bdef@c3fc3efa9";
        req.setParam("sign", MD5.Md5(theme_id+System.currentTimeMillis()/1000+key).toLowerCase());


        req.setTag(new ReqTag.Builder().build(2));
        FutureApi.getInstance().add(req);
    }

    private List<Theme> list;
    private CommonAdapter<Theme> adapter;
    private int currentPos;
    private int price;
    @Override
    public void onApiOnSuccess(ReqTag tag, Object data) {
        switch (tag.getReqId()) {
            case 1:
                //显示数据
                YuyueData yuyueData = (YuyueData) data;
                if(yuyueData.getAnchor_info()==null){
                    Toast.makeText(getContext(),"无效数据",Toast.LENGTH_SHORT).show();
                    dismiss();
                    return;
                }
                price = yuyueData.getAnchor_info().getPrice();
                ImageLoader.getInstance().displayImage(yuyueData.getAnchor_info().getAvatar(), (ImageView) findViewById(R.id.iv_head), ImageUtils.imgOptionsHead);
                ((TextView)findViewById(R.id.tv_name)).setText(yuyueData.getAnchor_info().getUsername());


                ((TextView)findViewById(R.id.tv_qinmizhi)).setText("我和她亲密值："+yuyueData.getClose_num());
                list = yuyueData.getList();
                if(list!=null && list.size()>0){

                    list.get(0).setSel(true);

                    adapter = new CommonAdapter<Theme>(getContext(),list,R.layout.item_yuyue) {
                        @Override
                        public void convert(BaseViewHolder holder, Theme itemData, int position, ViewGroup parent) {
                            TextView tv_title = holder.getView(R.id.tv_title);
                            TextView tv_time = holder.getView(R.id.tv_time);
                            TextView tv_price = holder.getView(R.id.tv_price);
                            tv_title.setText(itemData.getTheme());
                            tv_time.setText("时长："+itemData.getDuration()+"分钟");
                            tv_price.setText("消费："+itemData.getDuration()*yuyueData.getAnchor_info().getPrice()+"E币");
                            if(itemData.isSel()){
                                //已经选中
                                holder.getView(R.id.rl_yuyue).setBackgroundResource(R.drawable.bg_yuyue);
                                tv_title.setTextColor(Color.WHITE);
                                tv_time.setTextColor(Color.WHITE);
                                tv_price.setTextColor(Color.WHITE);
                            }else{
                                holder.getView(R.id.rl_yuyue).setBackgroundResource(R.drawable.bg_yuyue_normal);
                                tv_title.setTextColor(Color.parseColor("#666666"));
                                tv_time.setTextColor(Color.parseColor("#666666"));
                                tv_price.setTextColor(Color.parseColor("#666666"));
                            }

                        }
                    };

                    ListView listview = findViewById(R.id.listview);
                    listview.setAdapter(adapter);
                    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            list.get(currentPos).setSel(false);
                            list.get(position).setSel(true);
                            currentPos = position;
                            adapter.notifyDataSetChanged();

                        }
                    });
                }
                break;
            case 2:
                Toast.makeText(getContext(),"预约成功",Toast.LENGTH_SHORT).show();
                dismiss();
                if (listener != null) {
                    listener.onYesClick();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onApiFailure(ReqTag tag, MamaHaoServerError error, AbstractRequest.MamaHaoError clientError) {
        switch (tag.getReqId()) {
            case 1:
                //显示数据
                Toast.makeText(getContext(),"无效数据",Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case 2:
                Toast.makeText(getContext(),"预约失败",Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {

        if (v.getId()==R.id.bt_yes){
           if(list!=null && list.size()>0){
               if(MyApplication.getInstance().e_money<list.get(currentPos).getDuration()*price){
                   new YesOrNoDialog(getContext(), new YesOrNoDialogEntity("您的E币余额不够，不能与主播视频\n请及时充值", "我知道了", "立即充值"), new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                       @Override
                       public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                           switch (yesOrNoType) {
                               case BtnOk:
                                   RechargeActivity.openActivity(getContext(),RechargeActivity.class);
                                   break;

                               default:
                                   break;
                           }
                       }
                   }).show();
               }else{
                   requestYuyue();
               }

           }
        }if (v.getId()==R.id.iv_close){
            dismiss();
        }
    }

    public interface OnYuyueDialogClickListener {
        void onYesClick();
    }

}
